package com.crecheng.shop.controller;

import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.BrowserData;
import com.crecheng.shop.Manage.BrowserManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Time;
import java.sql.Timestamp;

@Controller
public class BrowserController {
    @RequestMapping("/browser/get")
    public @ResponseBody
    String getBrowserData(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                          @RequestParam(value = "count",defaultValue = "-1")int count){
        if(uid<1)
            return MyMessage.defaultError;
        else {
            if(count<1)
                return JSON.toJSONString(BrowserManage.getUserLast(uid,99999));
            else
                return JSON.toJSONString(BrowserManage.getUserLast(uid,count));
        }
    }

    @RequestMapping("/browser/add")
    public @ResponseBody
    String addBrowser(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                      @RequestParam(value = "sid",defaultValue = "-1")int sid){
        if(uid<1||sid<1)
            return MyMessage.defaultError;

        BrowserData data=new BrowserData();
        data.set(uid,sid,new Timestamp(System.currentTimeMillis()));
        if(BrowserManage.add(data))
            return MyMessage.defaultSuccess;
        else return MyMessage.defaultSqlError;
    }

    @RequestMapping("/browser/del")
    public @ResponseBody
    String delBrowser(@RequestParam(value = "uid",defaultValue = "-1")int uid,
                      @RequestParam(value = "id",defaultValue = "-1")int id){
        if(uid<1)
            return MyMessage.defaultError;
        else {
            if(id<1)
            {
                 int num= BrowserManage.delByUser(uid);
                 if(num>0)
                     return MyMessage.toJson("success",num);
                 else
                     return MyMessage.defaultSqlError;
            }
            else
            {
                if(BrowserManage.del(id))
                    return MyMessage.defaultSuccess;
                else
                    return MyMessage.defaultSqlError;
            }
        }
    }
}
